package com.qqtech.qquan.mall.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.mall.cache.MallShopCategoryCache;
import com.qqtech.qquan.mall.dao.MallShopCategoryDao;
import com.qqtech.qquan.mall.model.MallShopCategory;
import com.qqtech.qquan.mall.model.MallShopCategoryQuery;
import com.qqtech.qquan.mall.model.MallShopCategoryVo;

@Service
public class MallShopCategoryServiceImpl extends BaseServiceImpl<MallShopCategory>
		implements MallShopCategoryService {
	public static final Logger log = LoggerFactory.getLogger(MallShopCategoryServiceImpl.class);
	
	@Resource
	private MallShopCategoryDao mallShopCategoryDao;
	@Resource
	private MallShopCategoryCache mallShopCategoryCache;

	@Override
	protected BaseDao<MallShopCategory> getBaseDao() {
		return mallShopCategoryDao;
	}
	
	@Override
	public MallShopCategoryVo getByCategoryId(java.lang.String categoryId) {
		return mallShopCategoryDao.getByCategoryId(categoryId);
	}

	@Override
	public Integer getMaxOidxByParentId(String parentId) {
		if (StringUtil.isBlank(parentId)) {
			parentId = "0";
		}
		MallShopCategoryQuery mallShopCategoryQuery = new MallShopCategoryQuery();
		mallShopCategoryQuery.setParentId(parentId);
		Integer maxOidx = mallShopCategoryDao.getMaxOidx(mallShopCategoryQuery);
		if (null == maxOidx) {
			maxOidx = 0;
		}
		return maxOidx;
	}

	@Override
	public List<MallShopCategoryVo> queryListByParentId(String parentId,
			Integer status) {
		return queryListByParentId(parentId,status,null);
	}
	
	@Override
	public List<MallShopCategoryVo> queryListByParentId(String parentId,
			Integer status, Integer type) {
		if (StringUtil.isBlank(parentId)) {
			parentId = "0";
		}
		MallShopCategoryQuery mallShopCategoryQuery = new MallShopCategoryQuery();
		mallShopCategoryQuery.setParentId(parentId);
		mallShopCategoryQuery.setStatus(status);
		mallShopCategoryQuery.setType(type);
		Pageable pageKit = new PageKit(mallShopCategoryQuery.getPage(), Integer.MAX_VALUE, new PageOrder(Direction.ASC, "oidx"));
		return mallShopCategoryDao.queryList(mallShopCategoryQuery, pageKit);
	}
	
	@Override
	public List<MallShopCategoryVo> queryListByParentIdCache(String parentId) {
		if (StringUtil.isBlank(parentId)) {
			parentId = "0";
		}
		return mallShopCategoryCache.get(parentId);
	}

	@Override
	public List<MallShopCategoryVo> findAllParent(String categoryId) {
		if (StringUtil.isBlank(categoryId)) {
			return null;
		}
		return mallShopCategoryDao.findAllParent(categoryId);
	}

	@Override
	public int updateByCategoryId(MallShopCategory entity) {
		int v = mallShopCategoryDao.updateByCategoryId(entity);
		mallShopCategoryCache.clearAll();
		return v;
	}

	@Override
	public Page<MallShopCategoryVo> queryPageListAdmin(MallShopCategory query, Pageable pageable) {
		return mallShopCategoryDao.queryPageListAdmin(query, pageable);
	}

	@Override
	public List<MallShopCategoryVo> findParentByShop(Integer communityId, Integer style) {
		List<MallShopCategoryVo> returnList = null;
		Map<String,MallShopCategoryVo> map = null;
		List<MallShopCategoryVo> list = mallShopCategoryDao.findByShop(communityId,style);
		if (list != null) {
			map = new HashMap<String,MallShopCategoryVo>();
			for (MallShopCategoryVo temp : list) {
				String name = temp.getParentName();
				String categoryId = temp.getParentId();
				if (temp.getParentId().equals("0")) {
					name = temp.getName();
					categoryId = temp.getCategoryId();
				}
				if (!map.containsKey(categoryId)) {
					MallShopCategoryVo v = new MallShopCategoryVo();
					v.setCategoryId(categoryId);
					v.setName(name);
					map.put(categoryId, v);
				}
			}
			if (!map.isEmpty()) {
				Collection<MallShopCategoryVo> valueCollection = map.values();
				returnList = new ArrayList<MallShopCategoryVo>(valueCollection);  
			}
		}
		return returnList;
	}	
}